#!/usr/bin/env python
import irc
import imp
import sys
import glob

import debug
import config

fanhoso_metadata = {
	"version": (0,1), 
	"build": "20061004", 
	"copyright": "Core Security Research", 
	"year": "2006", 
	"authors": "Samuel Gordalina, Daniel Oliveira"
}


class fanhoso:
	def __init__(self, modpath="modules"):
		self.debug = debug.debug(True)
		self.modpath = modpath
		self.modules = []
		
		self.cfg = config.config(fanhoso_metadata)
		self.config = self.cfg.getConfig()
		self.irc = irc.client(self)

	def banner(self):
		print "fanhoso %s (build: %s)" % (self.config["fanhoso"]["version"], self.config["fanhoso"]["build"])
		print "Copyright (C) %s %s" % (self.config["fanhoso"]["year"], self.config["fanhoso"]["copyright"])
		print "Written by: %s" % (self.config["fanhoso"]["authors"])
		print ""

	def load_modules(self):
		for mod in glob.glob(self.modpath + '/mod_*.py'):
			name = mod.split("/")[1].split(".")[0]
			self.debug.info("Loading module %s", name)
			fp, pathname, description = imp.find_module(name, [self.modpath])
			try:
				# Load module
				fp_module = imp.load_module(name, fp, pathname, description)
				module = fp_module.module(self)
				self.modules.append(module)
			finally:
				if fp:
					fp.close()

	def start(self):
		self.load_modules()
		self.irc.connect()
		self.irc.parser()

# main
if __name__ == '__main__':
	bot = fanhoso()
	bot.banner()
	bot.start()
